package cn.darkhorse.admin.controller.workorder;

import cn.darkhorse.basic.domain.QmsSign;
import cn.darkhorse.basic.service.IQmsSignService;
import cn.darkhorse.workorder.SignTypeEnum;
import cn.darkhorse.workorder.domain.*;
import cn.darkhorse.workorder.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/workorder/signLog")
public class QmsSignLogController extends BaseController {
    @Resource
    private IQmsSignLogService qmsSignLogService;
    @Resource
    private IQmsSignService signService;
    @Resource
    private IQmsWorkorderService qmsWorkorderService;
    @Resource
    private IQmsWorkorderRecordService qmsWorkorderRecordService;
    @Resource
    private IQmsWorkorderAuditService qmsWorkorderAuditService;

    @Resource
    private IQmsWorkorderHviService qmsWorkorderHviService;

    @Resource
    private IQmsWorkorderHviRecordService qmsWorkorderHviRecordService;

    @Resource
    private IQmsWorkorderSenseService qmsWorkorderSenseService;

    @Resource
    private IQmsWorkorderSampleBalanceService qmsWorkorderSampleBalanceService;

    @Resource
    private IQmsWorkorderSampleDeliveryService qmsWorkorderSampleDeliveryService;

    @GetMapping("/list")
    @ApiOperation("列表")
    public TableDataInfo list(String orderNumber) {
        QueryWrapper<QmsSignLog> wrapper = new QueryWrapper<>();
        if (orderNumber != null){
            wrapper.eq("use_order_number", orderNumber);
        }
        wrapper.eq("create_by_id", getUserId());
        wrapper.orderByDesc("create_time");

        startPage();
        List<QmsSignLog> list = qmsSignLogService.list(wrapper);
        return getDataTable(list);
    }

//    @ApiOperation("签名")
//    @Log(title = "流转单签名", businessType = BusinessType.UPDATE)
//    @Transactional(rollbackFor = Exception.class)
//    @PostMapping("/sign")
//    public AjaxResult sign(@RequestBody QmsSignLog qmsSignLog)
//    {
//        QmsSign qmsSign = signService.getById(qmsSignLog.getSignId());
//        if (!qmsSign.getSignPassword().equals(qmsSignLog.getPassword())){
//            throw new RuntimeException("签名密码错误");
//        }
//
//        SignTypeEnum signTypeEnum = SignTypeEnum.getByCodeAndOrderStatus(qmsSignLog.getSignColumnName(), qmsSignLog.getOrderStatus());
//        Object result =  null;
//        if (signTypeEnum != null) {
//            result = signTypeEnum.getWrapper();
//        }
//        if (result != null){
//            if (result instanceof QmsWorkorderSampleDelivery){
//                UpdateWrapper<QmsWorkorderSampleDelivery> updateWrapper = new UpdateWrapper<>();
//                updateWrapper.eq("order_number", qmsSignLog.getUseOrderNumber());
//                updateWrapper.set(signTypeEnum.getColumn(), qmsSign.getSignPic());
//                qmsWorkorderSampleDeliveryService.update(updateWrapper);
//            }else if (result instanceof QmsWorkorderSense){
//                UpdateWrapper<QmsWorkorderSense> updateWrapper = new UpdateWrapper<>();
//                updateWrapper.eq("order_number", qmsSignLog.getUseOrderNumber());
//                updateWrapper.set(signTypeEnum.getColumn(), qmsSign.getSignPic());
//                qmsWorkorderSenseService.update(updateWrapper);
//            }else if (result instanceof QmsWorkorderSampleBalance){
//                UpdateWrapper<QmsWorkorderSampleBalance> updateWrapper = new UpdateWrapper<>();
//                updateWrapper.eq("order_number", qmsSignLog.getUseOrderNumber());
//                updateWrapper.set(signTypeEnum.getColumn(), qmsSign.getSignPic());
//                qmsWorkorderSampleBalanceService.update(updateWrapper);
//            }else if (result instanceof QmsWorkorderHvi){
//                UpdateWrapper<QmsWorkorderHvi> updateWrapper = new UpdateWrapper<>();
//                updateWrapper.eq("order_number", qmsSignLog.getUseOrderNumber());
//                updateWrapper.set(signTypeEnum.getColumn(), qmsSign.getSignPic());
//                qmsWorkorderHviService.update(updateWrapper);
//            }else if (result instanceof QmsWorkorderHviRecord){
//                UpdateWrapper<QmsWorkorderHviRecord> updateWrapper = new UpdateWrapper<>();
//                updateWrapper.eq("order_number", qmsSignLog.getUseOrderNumber());
//                updateWrapper.set(signTypeEnum.getColumn(), qmsSign.getSignPic());
//                updateWrapper.eq("id", qmsSignLog.getEntityId());
//                qmsWorkorderHviRecordService.update(updateWrapper);
//            }else if (result instanceof QmsWorkorderAudit){
//                UpdateWrapper<QmsWorkorderAudit> updateWrapper = new UpdateWrapper<>();
//                updateWrapper.eq("order_number", qmsSignLog.getUseOrderNumber());
//                updateWrapper.set(signTypeEnum.getColumn(), qmsSign.getSignPic());
//                qmsWorkorderAuditService.update(updateWrapper);
//            }
//            qmsSignLog.setOrderStatus(signTypeEnum.getOrderStatus());
//            qmsSignLog.setUseWhere(signTypeEnum.getColumnName());
//            qmsSignLogService.save(qmsSignLog);
//            return AjaxResult.success("签名成功");
//        }
//        return AjaxResult.error("签名失败");
//    }
@ApiOperation("签名")
@Log(title = "流转单签名", businessType = BusinessType.UPDATE)
@Transactional(rollbackFor = Exception.class)
@PostMapping("/sign")
public AjaxResult sign(@RequestBody QmsSignLog qmsSignLog)
{
    QmsSign qmsSign = signService.getById(qmsSignLog.getSignId());
    if (!qmsSign.getSignPassword().equals(qmsSignLog.getPassword())){
        throw new RuntimeException("签名密码错误");
    }

    SignTypeEnum signTypeEnum = SignTypeEnum.getByCodeAndOrderStatus(qmsSignLog.getSignColumnName(), qmsSignLog.getOrderStatus());
    if (signTypeEnum != null) {
        UpdateWrapper<QmsWorkorderRecord> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set(signTypeEnum.getColumn(), qmsSign.getSignPic());
        updateWrapper.eq("order_number", qmsSignLog.getUseOrderNumber());
        // 平衡的感官检验负责人，直接带入上一步感官的负责人
        if (signTypeEnum.getCode().equals("sensePrincipalSign")){
            SignTypeEnum senseInspectorSign = SignTypeEnum.getByCodeAndOrderStatus("senseInspectorSign", 2);
            if (senseInspectorSign != null) {
                updateWrapper.set(senseInspectorSign.getColumn(), qmsSign.getSignPic());
            }
        }
        qmsWorkorderRecordService.update(updateWrapper);
        return AjaxResult.success("签名成功");
    }

    return AjaxResult.success("签名失败");
}


}
